﻿using System;
using System.Web;
using System.Web.UI.WebControls;
using Core.AdminGroupMenuPermission;
using Core.Authenticated;
using Core.Library;
using DataObject.Tag;
using DataObject.TagDetails;

namespace CMS.Controls.Tag
{
    public partial class TagManager : AuthenticatedControl
    {
        #region Variables

        protected string Tab;
        protected string Url;
        protected int TypeId;
        protected int Publish;
        protected int PageIndex;
        protected int PageSize;

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            Tab = Utils.GetIdFromRequestString(Utils.Tab, string.Empty);

            if (string.IsNullOrEmpty(Tab))
                Response.Redirect(UrlHelpers.ListTab(cControl.AdminMenuId, Utils.IsPublished));

            PageIndex = Utils.GetIdFromRequestString(Utils.PageIndex, SettingValues.PageIndex);
            PageSize = Utils.GetIdFromRequestString(Utils.PageSize, ConvertUtility.ConvertTo<int>(ddlPageSize.SelectedValue));
            Url = HttpUtility.UrlEncode(Request.Url.ToString());

            if (!IsPostBack)
            {
                InitArticle();
                ListData();
                GetStatistics();
            }
        }

        #region Statistics

        private void GetStatistics()
        {
            Publish = TagController.Tag_GetCount();
        }

        #endregion

        #region Bind Data

        private void InitArticle()
        {
            var isShowPublished = false;

            var adminGroupMenuPermission =
                    AdminGroupMenuPermissionController.AdminGroupMenuPermission_GetBy_AdminUserId_AdminMenuId(CurrentUser.AdminUserId, cControl.AdminMenuId);
            if (adminGroupMenuPermission.Count > 0)
            {
                foreach (var agmp in adminGroupMenuPermission)
                {
                    isShowPublished = StatusHelper.IsChecked(agmp.Status, (int)PermissionObject.CanPublish);
                }
            }

            hfTab.Value = Tab;
            if (!string.IsNullOrEmpty(hfTab.Value))
            {
                switch (hfTab.Value)
                {
                    case "IsPublished":
                        hfStatus.Value = Core.Library.Status.IsPublished.ToString();
                        btnDelete.Visible = isShowPublished;
                        break;
                }
            }
            else
            {
                btnDelete.Enabled = false;
                ltMessage.Text = MessageUtility.ShowMessage(string.Empty, "Không lấy được dữ liệu.", MessageType.Failure);
            }
        }

        private void ListData()
        {

            lvList.DataSource = TagController.Tag_GetListPaging(PageIndex, PageSize);
            lvList.DataBind();

            var totalRecords = TagController.Tag_GetCount();

            ltPager.Text = PagerUtility.PageLinks(PageIndex, PageSize, totalRecords, 5,
                                UrlHelpers.ListSearch(cControl.AdminMenuId, hfTab.Value, "{0}", PageSize));

            ddlPageSize.SelectedValue = PageSize.ToString();
        }

        #endregion

        #region Action

        private void Delete()
        {
            ltMessage.Text = string.Empty;
            var count = 0;
            foreach (ListViewDataItem lvi in lvList.Items)
            {
                var cbItem = (CheckBox)lvi.FindControl("cbItem");
                if (cbItem == null || !cbItem.Checked) continue;

                count++;
                var id = ConvertUtility.ConvertTo<int>(lvList.DataKeys[lvi.DisplayIndex].Value);
                if (id > 0)
                {
                    var o = TagController.Tag_GetById(id);
                    if (o != null)
                    {
                        TagDetailsController.TagDetails_DeleteByTagId(id);
                        TagController.Tag_Delete(id);
                    }
                }
            }

            if (count == 0)
                ltMessage.Text = MessageUtility.ShowMessage(string.Empty, "Bạn chưa chọn bài viết nào.", MessageType.Warning);

            ListData();
            GetStatistics();
        }

        protected void BtnDelete(object sender, EventArgs e)
        {
            Delete();
        }

        protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            RedirectLink(ConvertUtility.ConvertTo<int>(ddlPageSize.SelectedValue));
        }

        public void RedirectLink(int currentPageSize)
        {

            Response.Redirect(UrlHelpers.ListSearch(cControl.AdminMenuId,
                                                        hfTab.Value,
                                                        SettingValues.PageIndex.ToString(), currentPageSize));
        }

        #endregion               
    }
}